/* 599. 两个列表的最小索引总和 */
/**
 * @param {string[]} list1
 * @param {string[]} list2
 * @return {string[]}
 */
var findRestaurant = function (list1, list2) {
    const result = []
    const map = new Map()
    for (let i = 0; i < list2.length; i++) {
        const index = list1.indexOf(list2[i])
        if (index >= 0) {
            map.set(list2[i], index + i)
        }
    }
    const temp = [...map].sort((a, b) => a[1] - b[1])
    for (let i = 0; i < temp.length; i++) {
        if (!result.length || temp[i][1] === temp[i - 1][1]) {
            result.push(temp[i][0])
        } else break
    }
    return result
};
var findRestaurant = function (list1, list2) {
    const index = new Map()
    for (let i = 0; i < list1.length; i++) {
        index.set(list1[i], i)
    }

    const ret = []
    let indexSum = Number.MAX_VALUE
    for (let i = 0; i < list2.length; i++) {
        if (index.has(list2[i])) {
            const j = index.get(list2[i])
            if (i + j < indexSum) {
                /* 置空数组 */
                ret.length = 0
                ret.push(list2[i])
                indexSum = i + j
            } else if (i + j === indexSum) {
                ret.push(list2[i])
            }
        }
    }
    return ret
}
findRestaurant(["Shogun", "Tapioca Express", "Burger King", "KFC"],
    ["KFC", "Shogun", "Burger King"])